﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_Trade")]
    public class T_Trade :IEntity<T_Trade>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int ArticleId { get; set; }

        /// <summary>
        /// 交易金额
        /// </summary>
        public decimal Momey { get; set; }

        /// <summary>
        /// 交易描述
        /// </summary>
        [Column(TypeName="text")]
        public string Description { get; set; }

        /// <summary>
        /// 状态
        /// 1：发起交易 
        /// 2：确认交易，待支付 
        /// 3：已付款，交易中 
        /// 4：交易完成，待确认（约稿人发起） 
        /// 5：交易完成，待确认（投稿人发起）
        /// 6：交易已确认
        /// 7：交易完成
        /// 8：有争议
        /// 99：交易取消
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 约稿人
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Supplier { get; set; }

        /// <summary>
        /// 投稿人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Consumer { get; set; }

        /// <summary>
        /// 交易发起时间
        /// </summary>
        public DateTime OpenDate { get; set; }

        /// <summary>
        /// 交易内容修改时间
        /// </summary>
        public DateTime LastModify { get; set; }

        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string Token { get; set; }

        /// <summary>
        /// 交易待确认发起时间
        /// </summary>
        public DateTime ConfirmTradeDate { get; set; }

        /// <summary>
        /// 确认超时时间。到达这个时间过后。交易自动完成
        /// </summary>
        public DateTime ConfirmExpireDate { get; set; }

        /// <summary>
        /// 交易完成时间
        /// </summary>
        public DateTime TradingDate { get; set; }

        [Column(TypeName="int")]
        public int PeriodicalId { get; set; }

        /// <summary>
        /// 发起人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string OpenUsername { get; set; }

        [Column(TypeName="bit")]
        public bool IsDelete { get; set; }

        /// <summary>
        /// 如果是行业投稿，标示行业Id
        /// </summary>
        public int Organization { get; set; }

        public override T_Trade Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Trade Find(System.Linq.Expressions.Expression<Func<T_Trade, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Trade.FirstOrDefault(where);
                if (item != null)
                {
                    this.ConfirmExpireDate = item.ConfirmExpireDate;
                    this.ConfirmTradeDate = item.ConfirmTradeDate;
                    this.Consumer = item.Consumer;
                    this.Description = item.Description;
                    this.Id = item.Id;
                    this.ArticleId = item.ArticleId;
                    this.LastModify = item.LastModify;
                    this.Momey = item.Momey;
                    this.OpenDate = item.OpenDate;
                    this.Status = item.Status;
                    this.Supplier = item.Supplier;
                    this.Token = item.Token;
                    this.TradingDate = item.TradingDate;
                    this.PeriodicalId = item.PeriodicalId;
                    this.OpenUsername = item.OpenUsername;
                    this.IsDelete = item.IsDelete;
                    this.Organization = item.Organization;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Trade, bool>> where)
        {
            using(Context ctx = new Context())
            {
                return ctx.Trade.Any(where);
            }
        }
    }
}
